package com.zzld.modules.autoinn.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;










import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.zzld.core.utils.MessageUtils;
import com.zzld.modules.autoinn.entity.Expense;
import com.zzld.modules.autoinn.entity.Recharge;
import com.zzld.modules.autoinn.service.IExpenseService;
import com.zzld.modules.common.dto.CommonJSON;
import com.zzld.modules.sys.security.ShiroUser;

@Controller
@RequestMapping("/autoinn/expense")
public class ExpenseController {

	@Autowired
	private IExpenseService expenseService;
	
	private String listurl = "/modules/autoinn/expense/expenseList";
	
	@RequestMapping(method = RequestMethod.GET)
	public String liq(){
	     return listurl;
	}
	
	@RequestMapping(value = "exportXls")
	public String exportXls(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) {
		Map map = new HashMap<String, Object>();
		
		
		map.put("nickname", null);
		map.put("level", null);
		map.put("starinnertime",null);
		map.put("endinnertime", null);
		map.put("sort", "innertime");
		map.put("order", "desc");

		ShiroUser userq = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		
		List<Expense> list = expenseService.selectall(map);
		modelMap.put(NormalExcelConstants.FILE_NAME, "消费信息表");
		modelMap.put(NormalExcelConstants.CLASS, Expense.class);
		modelMap.put(NormalExcelConstants.PARAMS, new ExportParams("消费信息表",
				"导出人:"+userq.getName(), "导出消费信息"));
		modelMap.put(NormalExcelConstants.DATA_LIST, list);
		return NormalExcelConstants.JEECG_EXCEL_VIEW;
		

	}
	
	
	@RequestMapping(value="delete/{id}",method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON del(Model model,HttpServletRequest request,
			@PathVariable("id") String id){
		CommonJSON json = new CommonJSON();
		try{
			if(expenseService.deleteByPrimaryKey(id)>0){
				json.setMessage(MessageUtils.DELETE_SUCCESS);
				json.setSuccess(true);
			}else{
				json.setMessage(MessageUtils.DELETE_WARNING);
				json.setSuccess(false);
			}
		}catch(Exception e){
			json.setMessage(MessageUtils.DELETE_WARNING);
			json.setSuccess(false);
		}
		return json;
	}

	@RequestMapping(value="list")
	@ResponseBody
	public CommonJSON list(Model model,HttpServletRequest request){
		CommonJSON json = new CommonJSON();
		Map<String,Object> map = new HashMap<String,Object>();
		
		map.put("nickname", request.getParameter("nickname"));
		map.put("level", request.getParameter("level"));
		map.put("starinnertime",request.getParameter("starinnertime"));
		map.put("endinnertime", request.getParameter("endinnertime"));
		map.put("sort", request.getParameter("sort"));
		map.put("order", request.getParameter("order"));
		map.put("phoneNum", request.getParameter("phoneNum"));
		map.put("storename", request.getParameter("storename"));
		
		
		
		Integer pageNum = Integer.parseInt(request.getParameter("page"));
		Integer pageSize = Integer.parseInt(request.getParameter("rows"));
		
		PageInfo page = expenseService.list(map, pageNum, pageSize);
		json.setRows(page.getList());
		json.setTotal(page.getTotal());
		return json;
	}
}
